
let md5utils = require('./libs/md5');

cc.Class({
    extends: cc.Component,

    properties: {
        ed_username:cc.EditBox,
        ed_password:cc.EditBox,
        ed_email:cc.EditBox,
        ed_desc:cc.EditBox,
        ed_key:cc.EditBox,
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {

    },


    btn_backClicked(){
        this.node.destroy();
    },

    btn_addClicked(){
        let cur_username = this.getEditBoxText(this.ed_username);
        let cur_password = this.getEditBoxText(this.ed_password);
        let cur_email = this.getEditBoxText(this.ed_email);
        let cur_desc = this.getEditBoxText(this.ed_desc);
        let cur_key = this.getEditBoxText(this.ed_key);

        if(!cur_username){
            G.showToast('用户名不能为空!');
            return;
        }

        if(!cur_password){
            G.showToast('密码不能为空!');
            return;
        }

        // TODO: 暂时不验证格式
        if(!cur_email){
            G.showToast('邮箱不能为空!');
            return;
        }

        if(!cur_desc){
            G.showToast('描述不能为空!');
            return;
        }

        if(!cur_key){
            G.showToast('key不能为空!');
            return;
        }

        if(md5utils.hex_md5(cur_key) !== G.allData.key){
            G.showToast('请输入正确的key!');
            return;
        }

        G.netApi.item_add({
            username:cur_username,
            password:cur_password,
            email:cur_email,
            desc:cur_desc,
            uuid:G.allData.uuid,
            key:cur_key,
        },(err,data)=>{
            if(err){
                G.showToast('添加失败! err:' + err);
            }else{

                if(data.err){
                    G.showToast(data.msg);
                }else{
                    G.showToast('添加成功!');
                    let newRow = data.newRow;
                    G.allData.rows.push(newRow);

                    // 导航到
                    G.gameCtrl.naviToAllItem(G.allData);
                }
            }
        });
    },

    getEditBoxText(editbox){
        return editbox.string.trim();
    }
});
